package practise;

public class MergeTwoLists {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if(list2 == null && list1 == null) {
            return null;
        }
        if(list2 == null) {
            return list1;
        }
        if(list1 == null) {
            return list2;
        }

        ListNode head = new ListNode();
        ListNode curHead = head;
        ListNode cur1 = list1;
        ListNode cur2 = list2;


        while(cur2 != null && cur1 != null) {
            if(cur2.val > cur1.val) {
                curHead.next = cur1;
                cur1 = cur1.next;
            } else {
                curHead.next = cur2;
                cur2 = cur2.next;
            }
            curHead = curHead.next;
        }
        if(cur2 != null) {
            curHead.next = cur2;
        } else {
            curHead.next = cur1;
        }
        return head.next;
    }
}










